package com.wisdytech.linkdcs;

import com.wisdytech.common.netty.WebSocketServer;
import io.netty.channel.ChannelFuture;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.InetSocketAddress;

@SpringBootApplication(scanBasePackages="com.wisdytech")
@EnableTransactionManagement //事务支持
@MapperScan("com.wisdytech.linkdcs.**.dao**")
@EnableScheduling //定时任务支持
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class LinkdcsScadaApplication implements CommandLineRunner{


	private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);


	@Value("${websocket.port}")
	private int port;

	@Autowired
	WebSocketServer webSocketServer;


	public static void main(String[] args) {
		SpringApplication.run(LinkdcsScadaApplication.class, args);
	}

	@Override
	public void run(String... strings) throws Exception {
		InetSocketAddress address = new InetSocketAddress("127.0.0.1", port);
		ChannelFuture future = webSocketServer.start(address);
		log.info("websocket start at port[{}]",port);
		Runtime.getRuntime().addShutdownHook(new Thread(() -> webSocketServer.destroy()));
		future.channel().closeFuture().syncUninterruptibly();
	}
}
